<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./../../_template.xhtml"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core">

    <ui:define name="centro">
        <h:form>

            <p:outputLabel value="Cadastro de Nota Fiscal - Incluir" rendered="#{empty notaFiscalBean.notaFiscal.id}" 
                           style="font-size: x-large; width: 60%; margin-left: 25px" styleClass="labelInserir" />
            <p:outputLabel value="Cadastro de Nota Fiscal - Alterar" rendered="#{not empty notaFiscalBean.notaFiscal.id}"
                           style="font-size: x-large; width: 60%; margin-left: 25px" styleClass="labelInserir" />
            <p:fieldset styleClass="fieldsetInsert" style="width: 92%; margin-left: 25px">
                <p:messages />
                <h:panelGrid columns="5" style="margin-bottom: 10px" >
                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Número" />
                        <p:inputText size="2" value="#{notaFiscalBean.notaFiscal.numero}" 
                                     style="width: 100px; margin-left: 20px;text-align: right" 
                                     required="true" requiredMessage="Informe o número da Nota Fiscal" />
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel  style="margin-left: 20px; text-align: left" value="Data da Emissão"/>
                        <p:calendar pattern="dd/MM/yyyy" yearRange="c-100:c+100" navigator="true" readonlyInput="true" 
                                    style="margin-left: 20px;" maxdate="#{clienteBean.dataAtual}" 
                                    value="#{notaFiscalBean.notaFiscal.dataEmissao}" 
                                    required="true" requiredMessage="Informe a data de Emissão." />
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel  style="margin-left: 20px; text-align: left" value="Tipo de Nota"/>
                        <p:selectOneMenu value="#{notaFiscalBean.notaFiscal.tipoNotaFiscal}" converter="entityConverter"
                                         required="true" requiredMessage="Informe o tipo de Nota Fiscal" style="margin-left: 20px">
                            <f:selectItem itemLabel="Escolha uma opção"/>
                            <f:selectItems value="#{tipoNotaFiscalBean.tipoNotasFiscais}" var="tipoNF" itemLabel="#{tipoNF.descricao}"/>
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Tipo de Cliente" />
                        <p:selectOneRadio id="tpCliente" value="#{notaFiscalBean.origem}" style="margin-left: 20px;"  >
                            <f:selectItem itemLabel="PF"   itemValue="CLIENTE_PF"/>
                            <f:selectItem itemLabel="PJ"   itemValue="CLIENTE_PJ"/>
                            <f:ajax render="cliente" execute="tpCliente" listener="#{notaFiscalBean.buscaClientePorTipo}"/>
                        </p:selectOneRadio>  
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 25px; text-align: left" value="Nome" />
                        <p:selectOneMenu id="cliente" value="#{notaFiscalBean.notaFiscal.cliente}" converter="entityConverter"
                                         style="margin-left: 25px; width: 250px">
                            <f:selectItem itemLabel="Escolha uma opção"/>
                            <f:selectItems value="#{notaFiscalBean.clientes}" var="lista" itemLabel="#{lista.nome}" />
                        </p:selectOneMenu>
                    </h:panelGrid>
                </h:panelGrid>

                <h:panelGrid columns="6" style="margin-bottom: 10px" >
                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Valor Bruto" />
                        <p:inputText styleClass="moeda"  value="#{notaFiscalBean.notaFiscal.valorBruto}" style="margin-left: 20px; width: 150px; text-align: right"
                                     size="18" required="true" requiredMessage="Informe o valor bruto." >
                            <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR" />
                            <p:ajax listener="#{notaFiscalBean.receberValorBruto}" update="retencao, imposto"/>
                        </p:inputText>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Retenção" />
                        <p:inputText id="retencao" style="margin-left: 20px; width: 100px; text-align: right" value="#{notaFiscalBean.notaFiscal.retencao}" 
                                     size="10" required="true" requiredMessage="Informe a aliquota de retenção.">
                            <f:convertNumber type="percent" />
                            <p:ajax listener="#{notaFiscalBean.calcularRetencao}" update="vlrRet, vlrLiq" />
                        </p:inputText>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Valor Retido" />
                        <p:inputText id="vlrRet" styleClass="moeda" size="18" disabled="true" style="margin-left: 20px; width: 150px; text-align: right"
                                     value="#{notaFiscalBean.valorRetencao}" >
                            <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR" />
                            <p:ajax update="@this" />
                        </p:inputText>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Imposto Único" />
                        <p:inputText id="imposto" style="margin-left: 20px; width: 100px; text-align: right" value="#{notaFiscalBean.notaFiscal.aliquotaImpostoUnico}" 
                                 size="10" required="true" requiredMessage="Informe a aliquota do imposto único." >
                              <f:convertNumber type="percent" />
                              <p:ajax listener="#{notaFiscalBean.calcularImpostoUnico}" update="vlrImp, vlrLiq" />
                        </p:inputText>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left"  value="Valor do Imposto" />
                        <p:inputText id="vlrImp" styleClass="moeda" size="18" disabled="true"
                                     value="#{notaFiscalBean.valorImposto}"
                                     style="margin-left: 20px; width: 150px; text-align: right">
                            <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR"  />
                            <p:ajax update="@this" />
                        </p:inputText>
                    </h:panelGrid>
                    
                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Valor Líquido" />
                        <p:inputText id="vlrLiq" styleClass="moeda" size="18" disabled="true" style="margin-left: 20px; width: 150px; text-align: right"
                                     value="#{notaFiscalBean.valorBruto - notaFiscalBean.valorRetencao - notaFiscalBean.valorImposto}" >
                            <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR" />
                            <p:ajax update="@this" />
                        </p:inputText>
                    </h:panelGrid>

                </h:panelGrid>

                <h:panelGrid columns="3" style="margin-bottom: 10px" >
                    <h:panelGrid columns="1" >
                        <p:outputLabel  style="margin-left: 20px; text-align: left" value="Data do Vencimento"/>
                        <p:calendar pattern="dd/MM/yyyy" yearRange="c-100:c+100" navigator="true" readonlyInput="true" 
                                    style="margin-left: 20px;" mindate="#{clienteBean.dataAtual}"
                                    value="#{notaFiscalBean.notaFiscal.dataVencimento}" 
                                    required="true" requiredMessage="Informe a data do vencimento." />

                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel  style="margin-left: 20px; text-align: left" value="Conta para Crédito"/>
                        <p:selectOneMenu value="#{notaFiscalBean.notaFiscal.documento.conta}" converter="entityConverter"
                                         required="true" requiredMessage="Informe a conta para crédito."
                                         style="margin-left: 20px;">
                            <f:selectItem itemLabel="Escolha uma opção" noSelectionOption="true" />
                            <f:selectItems value="#{contaBean.contas}" var="conta" itemLabel="#{conta.nome}"/>
                        </p:selectOneMenu>
                    </h:panelGrid>

                    <h:panelGrid columns="1" >
                        <p:outputLabel style="margin-left: 20px; text-align: left" value="Grupo de Lançamento" />
                        <p:selectOneMenu value="#{notaFiscalBean.notaFiscal.documento.grupoLancamento}" converter="entityConverter" 
                                         style="margin-left: 20px; width: 200px" 
                                         required="true" requiredMessage="Selecione o grupo de lançamento" >
                            <f:selectItem itemLabel="selecione"/>
                            <f:selectItems value="#{grupoLancamentoBean.grupoLancamentos}" var="grupo" itemLabel="#{grupo.descricao}"/>
                        </p:selectOneMenu>
                    </h:panelGrid>
                </h:panelGrid>
                <br/>
                <p:commandButton value="Visualizar Lançamentos" action="#{notaFiscalBean.inserirDocumento()}" ajax="false"
                                 style="margin-left: 20px;" /><br />
                <p:commandButton value="Gravar" action="#{notaFiscalBean.gravar}" ajax="false"
                                 style="margin-left: 15px; margin-top: 30px"/>      
                <p:commandButton value="Voltar" action='#{notaFiscalBean.voltar}' immediate="true" ajax="false" 
                                 style="margin-left: 20px; margin-top: 30px"/>
            </p:fieldset>
            <br/>
            <h:panelGrid rendered="#{not empty notaFiscalBean.notaFiscal.documento.lancamentos}">
                <p:dataTable styleClass="dados" value="#{notaFiscalBean.notaFiscal.documento.lancamentos}" var="lcto"  >
                    <p:column style="width: 10px; text-align: center" headerText="Número do Lançamento"  >
                        <h:outputText value="#{lcto.sequencial}" />
                    </p:column>
                    <p:column style="width: 10px; text-align: center" headerText="Data de Pagamento"  >
                        <h:outputText value="#{lcto.dataPagamento}"  >
                            <f:convertDateTime pattern="dd/MM/yyyy" />
                        </h:outputText>
                    </p:column>
                    <p:column style="width: 10px; text-align: right" headerText="Valor"  >
                        <h:outputText value="#{lcto.valor}" >
                            <f:convertNumber minFractionDigits="2" maxFractionDigits="2" locale="pt_BR"  />
                        </h:outputText>
                    </p:column>
                    <p:column style="width: 10px; text-align: center" headerText="Tipo"  >
                        <h:outputText value="#{lcto.tipo}"  />
                    </p:column>
                    <p:column style="width: 10px; text-align: center" headerText="Situação"  >
                        <h:outputText value="#{lcto.situacao}"  />
                    </p:column>
                    <p:column style="width: 10px; text-align: center" headerText="Alterar" >
                        <h:commandLink value="A" action="#{notaFiscalBean.editarLancamento(lcto)}" style="text-align: center"/>
                    </p:column>
                </p:dataTable>
            </h:panelGrid>
        </h:form>
    </ui:define>

</ui:composition>
